package AccountSystem;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;


public class virtualWallet {
	
//	 1. 钱包暂时只支持充值、提现、支付、查询余额、查询交易流水这五个核心的功能，
//  其他比如冻结、透支、转赠等不常用的功能，可暂不考虑。

	private String v_id;
	private int balance;
	private String walletPassword;
	private String walletUsername;
	private ArrayList<String> al;
	
//充值时需要用户提供银行卡号及密码
	virtualWallet(String walletUsername,String walletPassword)
	{
		this.walletUsername=walletUsername;
		this.walletPassword=walletPassword;
		dataBaseAction dba = new dataBaseAction();
		HashMap <String,Object>hm = dba.getAccount(walletUsername, walletPassword);
		this.v_id=hm.get("v_id").toString();
		this.balance=Integer.parseInt(hm.get("balance").toString());
		this.al=(ArrayList<String>) hm.get("history");

	}
	
	
	public boolean Topup(int count,String userBankNumber,String userBankPassword) {

		if(bankAccountAction.transferMoneyUser2Company(count,userBankNumber,userBankPassword)==true)
		{
			this.balance+=count;
			SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
			al.add("Type:Topup"+"\t"+"Amount:"+String.valueOf(count)+"\t"+"Date:"+df.format(System.currentTimeMillis()));
			return true;
		}
		else return false;
	}
	
	public boolean Withdraw(int count,String userBankNumber)
	{
		if(bankAccountAction.transferMoneyCompany2User(count,userBankNumber)==true)
		{
			this.balance-=count;
			SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
			al.add("Type:Withdraw"+"\t"+"Amount:"+String.valueOf(count)+"\t"+"Date:"+df.format(System.currentTimeMillis()));
			return true;
		}
		else return false;
	}
	
	
	public int Retbalance()
	{
		return this.balance;
	}
	
	
	public boolean Payment(String password,int goodsPrice)
	{
		if(this.walletPassword.equals(password))
		{
			this.balance-=goodsPrice;
			SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
			al.add("Type:Payment"+"\t"+"Amount:"+String.valueOf(goodsPrice)+"\t"+"Date:"+df.format(System.currentTimeMillis()));
			return true;
		}
		else return false;
	}
	
	
	public ArrayList<String> history(){
		return al;
	}
}
